const path = require('path');
const webpackMerge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const baseConfig = require('./webpack.config.base.js');
const projectPath = require('./projectPath.js');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const workboxWebpackPlugin = require('workbox-webpack-plugin');
const prodConfig = {
  output: {
    filename: 'js/built.[name].[contenthash:10].js'
  },
  mode: 'production',
  module: {
    rules: [
      {
        oneOf: [
          {
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            use: ['babel-loader']
          },
          {
            test: /\.css$/,
            use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']
          }
        ]
      }
    ]
  },
  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(projectPath, 'public', 'favicon.ico'),
          to: path.resolve(projectPath, 'dist', 'favicon.ico'),
        }
      ]
    }),
    new MiniCssExtractPlugin({
      filename: 'built.[contenthash:10].css'
    }),//提取css为单独文件
    new OptimizeCssAssetsWebpackPlugin(),//压缩css
    new HtmlWebpackPlugin({
      title: 'webpack html',
      template: path.resolve(projectPath, 'public', 'template.html'),
      filename: 'index.html',
      minify: {//压缩选项
        removeComments: true,//去注释
        collapseWhitespace: true,//去空白符换行
        minifyCss: true//压缩内联样式
      }
    }),
    new workboxWebpackPlugin.GenerateSW({//PWA
      clientsClaim: true,
      skipWaiting: true
    })
  ]
}
module.exports = webpackMerge(baseConfig, prodConfig);